package cn.etynit.service.impl;

import cn.etynit.domain.Expense;
import cn.etynit.domain.User;
import cn.etynit.exception.GlobalExceptionCodeEnum;
import cn.etynit.mapper.ExpenseMapper;
import cn.etynit.mms.feign.UserService;
import cn.etynit.result.JsonResult;
import cn.etynit.service.IExpenseService;
import cn.etynit.util.AssertUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mysql.cj.protocol.x.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author etyn
 * @since 2022-12-09
 */
@Service
public class ExpenseServiceImpl extends ServiceImpl<ExpenseMapper, Expense> implements IExpenseService {

    @Autowired
    private UserService userService;

    private static final String NOTICE_OF_WATER_BILL_PAYMENT = "亲爱的业主：%s，您当月的水费为：%s，我们会在月初进行一次性扣除，若余额不足，请及时充值！";
    private static final String NOTICE_OF_ELECTRICITY_PAYMENT = "亲爱的业主：%s，您当月的电费为：%s，我们会在月初进行一次性扣除，若余额不足，请及时充值！";
    private static final String NOTICE_OF_GAS_FEE_PAYMENT = "亲爱的业主：%s，您当月的燃气费为：%s，我们会在月初进行一次性扣除，若余额不足，请及时充值！";
    private static final String NOTICE_OF_PARKING_FEE_PAYMENT = "亲爱的业主：%s，您当月的停车费为：%s，我们会在月初进行一次性扣除，若余额不足，请及时充值！";
    private static final String THERE_IS_NO_UNPAID_MESSAGE = "当前没有未缴费的业主";

    /**
     * 根据userId和缴费类型查询账单
     * @param type
     * @param userId
     * @return
     */
    @Override
    public Expense inquireCost(Integer type,Long userId,Integer month) {
        Wrapper<Expense> wrapper = new EntityWrapper<>();
        wrapper.eq("type",type);
        wrapper.eq("user_id",userId);
        wrapper.eq("month",month);
        return selectOne(wrapper);
    }

    /**
     * 费用自动推送
     * @return
     */
    @Override
    public List<String> adviceCharge() {
        List<Expense> expenses = selectList(null);
        List<String> message = new ArrayList<>();
        List<Expense> tmp = expenses.stream().filter(m -> (m.getPay() == -1)).collect(Collectors.toList());
        if (tmp.isEmpty()){ // 没有未缴费业主
            message.add(THERE_IS_NO_UNPAID_MESSAGE);
        } else { // 已缴费业主
            for (Expense expens : tmp) {
                // 根据id获取user对象
                JsonResult jsonResult = userService.get(expens.getUserId());
                AssertUtil.isTrue(jsonResult.isSuccess(), GlobalExceptionCodeEnum.ERROR);
                User user = JSON.parseObject(JSONObject.toJSONString(jsonResult.getData()), User.class);
                // 判断费用类型
                if (expens.getType() == 1){
                    message.add(String.format(NOTICE_OF_WATER_BILL_PAYMENT, user.getName(), expens.getMoney().toString()));

                } else if (expens.getType() == 2){
                    message.add(String.format(NOTICE_OF_ELECTRICITY_PAYMENT, user.getName(), expens.getMoney().toString()));

                } else if (expens.getType() == 3){
                    message.add(String.format(NOTICE_OF_GAS_FEE_PAYMENT, user.getName(), expens.getMoney().toString()));

                } else if (expens.getType() == 4) {
                    message.add(String.format(NOTICE_OF_PARKING_FEE_PAYMENT, user.getName(), expens.getMoney().toString()));
                }
            }
        }
        return message;
    }
}
